<template>
  <div class="customer-detail-container">
    <el-tabs v-model="activeTab" type="card">
      <el-tab-pane label="基础信息" name="basic">
        <customer-basic-tab-page :customer-id="customerId" @basicInfoUpdated="handleBasicInfoUpdated" />
      </el-tab-pane>
      <el-tab-pane label="往来交易记录" name="transactions">
        <customer-transaction-page :customer-id="customerId" />
      </el-tab-pane>
    </el-tabs>

    <!-- 编辑对话框 
    <customer-basic-info-dialog
      ref="editDialog"
      :visible.sync="showEditDialogVisible"
      :customer-data="currentCustomer"
      mode="edit"
      @submit="handleEditSubmit"
    />
    -->
  </div>
</template>

<script>
import CustomerBasicTabPage from './customerBasicTabPage.vue'
import CustomerTransactionPage from '../metal/customerTransactionsPage.vue'
//import CustomerBasicInfoDialog from './customerBasicInfoDialog.vue'
import { ElMessage } from 'element-plus'
import { ActionResultType, ActionResult } from '../../public/engine/common/actionResult'
import TabUtil from '@/plugins/tab'
import EventBus from '../../public/engine/ipc/eventBus'

export default {
  name: 'CustomerDetailPage',
  components: {
    CustomerBasicTabPage,
    CustomerTransactionPage,
    //  CustomerBasicInfoDialog
  },
  data() {
    return {
      activeTab: 'basic',
      customerId: '',
      currentCustomer: null,
      showEditDialogVisible: false,

      // 返回数据
      retValue: null
    }
  },
  created() {
    this.customerId = this.$route.query.id
    if (!this.customerId) {
      ElMessage.error('客户ID不存在')
      TabUtil.closePage()
    }
  },
  methods: {
    handleBasicInfoUpdated(customer) {
      this.currentCustomer = customer
      this.retValue = new ActionResult({ type: ActionResultType.UpdateCommon });
    },
    handleEditSubmit({ type }) {
      if (type === 'update') {
        ElMessage.success('客户信息更新成功')
        // 这里可以触发子组件刷新数据
      }
    }
  },

    // 卸载页面
  beforeUnmount() {
    //console.log('---> customerDetailPage beforeUnmount');
    if (this.retValue) {
      EventBus.emit('customerUpdated', this.retValue.toCopyJson());
      this.retValue = null;
    }
  },

  // 页面被隐藏时触发
  deactivated() {
    //console.log('---> customerDetailPage deactivated');
    if (this.retValue) {
      EventBus.emit('customerUpdated', this.retValue.toCopyJson())
      this.retValue = null;
    }
  },
}
</script>

<style scoped>
.customer-detail-container {
  padding: 15px;
  max-width: 1000px;
  margin-right: auto;
}
</style>